package com.shushan.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shushan.exam.bean.ExerciseBean;
import com.shushan.exam.bean.ExerciseOptionBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;

import java.util.List;


/**
 * @ClassName: AnswerAdapter
 * @Desciption: 练习详情答案选项列表的adapter
 * @author: yichaohua
 * @date: 2018-02-02
 */
public class ExerciseRecordAnswerAdapter extends RecyclerView.Adapter<ExerciseRecordAnswerAdapter.ViewHolder> {

    private static final String TAG = "AnswerAdapter";

    private Context context;
    private ExerciseBean exerciseBean;
    private List<ExerciseOptionBean> exerciseOptionList;
    private String[] aLabels;

    public ExerciseRecordAnswerAdapter(Context context, ExerciseBean exerciseBean) {
        this.context = context;
        this.exerciseBean = exerciseBean;
        this.exerciseOptionList = exerciseBean.getOptionList();

        aLabels = context.getResources().getStringArray(R.array.exam_answer_label_array);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.exam_answer_list_item, null, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ExerciseOptionBean exerciseOptionBean = exerciseOptionList.get(position);

        holder.labelTextView.setText(aLabels[position]);
        if (exerciseOptionBean.content_type.equals("TEXT")) {
            holder.answerTextView.setVisibility(View.VISIBLE);
            holder.answerTextView.setText(exerciseOptionBean.option_content);
            holder.answerTextImg.setVisibility(View.GONE);
        } else {
            holder.answerTextView.setVisibility(View.GONE);
            holder.answerTextImg.setVisibility(View.VISIBLE);
            if (holder.answerTextView.getTag() == null || (Integer) holder.answerTextView.getTag() != position) {
                ImageUtils.loadImageNoBg(context, exerciseOptionBean.option_imgs, holder.answerTextImg);
                holder.answerTextView.setTag(position);
            }
        }
        if(exerciseBean.my_answer_id == exerciseOptionBean.id){
            holder.answerImageView.setVisibility(View.VISIBLE);
            if(exerciseOptionBean.option_result == 1){
                holder.answerImageView.setImageResource(R.drawable.exam_answer_hook);
            }else{
                holder.answerImageView.setImageResource(R.drawable.exam_answer_wrong);
            }
        }
    }

    @Override
    public int getItemCount() {
        return exerciseOptionList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView labelTextView;
        ImageView answerImageView;
        TextView answerTextView;
        ImageView answerTextImg;

        public ViewHolder(View itemView) {
            super(itemView);
            labelTextView = itemView.findViewById(R.id.exam_answer_label);
            answerImageView = itemView.findViewById(R.id.exam_answer_img);
            answerTextView = itemView.findViewById(R.id.exam_answer_tv);
            answerTextImg = itemView.findViewById(R.id.exam_answer_text_img);
        }
    }

    public List<ExerciseOptionBean> getExerciseOptionList() {
        return exerciseOptionList;
    }

    public void setExercisesOptionList(List<ExerciseOptionBean> exerciseOptionList) {
        this.exerciseOptionList = exerciseOptionList;
    }

}
